"""数据库模块"""
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# 直接写明MySQL连接参数
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:Zktw%402025@111.198.53.163:58842/twrpa"

engine = create_engine(
    SQLALCHEMY_DATABASE_URL,
    pool_pre_ping=True,
    pool_recycle=3600
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

# 初始化数据库
def init_db():
    from ..models import shop  # 导入模型以创建表
    Base.metadata.create_all(bind=engine) 